package com.song.Inet.Socket1;



import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author ZhangGuisong
 * @Date 2021/5/4-10:14
 */
public class SocketServer {
    //监听的端口
    public static void main(String[] args) throws IOException {
        //设置监听的端口
        ServerSocket serverSocket=new ServerSocket(1234);
        //获取Socket,现在这个socket.是在阻塞的状态，等待客户端的请求
        System.out.println("服务端正在等待请求.....");
        Socket socketServer = serverSocket.accept();
        InputStream inputStream = socketServer.getInputStream();
        //把输入流转换成字节流
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
        String s = bufferedReader.readLine();
        /* byte[] bytes=new byte[1024];
        int len=0;
      while ((len=inputStream.read(bytes))!=-1) {
          System.out.println(new String(bytes,0,len));
      }*/
        System.out.println(s);
      //服务端接收到请求，给客户端一个回应
        OutputStream outputStream = socketServer.getOutputStream();
//        outputStream.write("this is socketServer".getBytes());
//        socketServer.shutdownOutput();
        //将字符输入流转换成字节流
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("hello this is server");
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();
        bufferedReader.close();
        inputStream.close();
        socketServer.close();


    }

}
